package com.example.kcb;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class CourseRecordAdapter extends ArrayAdapter<CourseRecords> {
    private Context context;
    private int layoutResourceId;

    public CourseRecordAdapter(Context context, int resource) {
        super(context, resource);
        this.context = context;
        this.layoutResourceId = resource;
    }

    public CourseRecordAdapter(Context context, int resource, List<CourseRecords> objects) {
        super(context, resource, objects);
        this.context = context;
        this.layoutResourceId = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        CourseRecords record = getItem(position);

        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(layoutResourceId, parent, false);
        } else {
            view = convertView;
        }

        if (view != null) {
            TextView tvCourseName = (TextView) view.findViewById(R.id.tv_course_name);
            TextView tvTeacher = (TextView) view.findViewById(R.id.tv_teacher);
            TextView tvClassRoom = (TextView) view.findViewById(R.id.tv_class_room);
            TextView tvPeriod = (TextView) view.findViewById(R.id.tv_period);
            TextView tvDate = (TextView) view.findViewById(R.id.tv_date);
            TextView tvCreateDate = (TextView) view.findViewById(R.id.tv_create_date);

            tvCourseName.setText(record.getCourseName());
            tvTeacher.setText(record.getTeacher());
            tvClassRoom.setText(record.getClassRoom());
            tvPeriod.setText(record.getPeriod());
            tvDate.setText(convert(record.getDay()));
            tvCreateDate.setText(record.getRecordTime());
        }

        return view;
    }

    private String convert(int day) {
        String result = "";
        switch (day){
            case 1:
                result = "周一";
                break;
            case 2:
                result = "周二";
                break;
            case 3:
                result = "周三";
                break;

            case 4:
                result = "周四";
                break;

            case 5:
                result = "周五";
                break;
            case 6:
                result = "周六";
                break;
            case 7:
                result = "周日";
                break;
            default:
                result = "";
                break;
        }
        return result;
    }
}
